package com.chy.controller;

import com.chy.bean.User;
import com.chy.service.LoginService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserLoginController {

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public  String userLogin(User user, Model model,HttpServletRequest request){
        boolean result = true;
        //执行登录操作
       Boolean flag = SpringUtil.applicationContext().getBean(LoginService.class).login(user);
        if (flag==result){
            System.out.println("UserLoginController.userLogin");
            request.getSession().setAttribute("USER",user);
            return "redirect:/order/list";
        }else {
            model.addAttribute("msg","账号密码有误，请重新登录");
            return "redirect:/user/login";
        }
    }

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String toLogin(HttpSession session) {
        session.invalidate();
        return "redirect:/user/UserLogin";
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/user/UserLogin";
    }

}
